package com.kgcx.organization.service;

import com.github.pagehelper.PageHelper;
import com.kgcx.common.bean.RequestData;
import com.kgcx.common.constant.Cons;
import com.kgcx.dbutils.domain.BaseDomain;
import com.kgcx.dbutils.utils.PageUtils;
import com.kgcx.organization.domain.Auto;
import com.kgcx.organization.mapper.AutoMapper;
import com.kgcx.organization.rabbitmq.producer.AutoProducer;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AutoService {
    @Autowired
    private AutoMapper autoMapper;
    @Autowired
    private AutoProducer autoProducer;
    @Resource
    private InsuranceService insuranceService;


    @Autowired
    private DriverService driverService;

    public Auto getAuto(Long kid) {
        return autoMapper.selectByPrimaryKey(kid);
    }

    public List<Auto> getAutoList(RequestData rd) {
        PageUtils.page(rd);
        PageHelper.orderBy(rd.getOrderBy());
        return autoMapper.selectActive(BaseDomain.buildSelectDomain(rd, new Auto()));
    }

    /**
     * 给监管平台查询车辆信息保险信息
     * @param rd
     * @return
     */
    public List<Auto> queryInsuranceWithPage(RequestData rd) {
        Auto auto = new Auto();
        auto.setCityId(rd.getLong("cityId"));
        auto.setSupplierId(rd.getLong("supplierId"));
        auto.setAutoNo(rd.getString("autoNo"));
        auto.setOwnerName(rd.getString("ownerName"));
        auto.setSource(rd.getInteger("source"));
        PageUtils.page(rd);
        PageHelper.orderBy(rd.getOrderBy());
        List<Auto> list = autoMapper.selectActive(auto);
        for (Auto at : list) {
            //如果有保险订单号 则表示有车辆保险
            if (!StringUtils.isBlank(at.getInsurNum())) {
                insuranceService.addCarInsurance(at);
            }
        }
        return list;
    }

    /**
     * 给监管平台查询车辆信息
     *
     * @param rd
     * @return
     */
    public List<Auto> queryAutoWithPage(RequestData rd) {

        Auto auto = new Auto();
        auto.setCityId(rd.getLong("cityId"));
        auto.setSupplierId(rd.getLong("supplierId"));
        auto.setAutoNo(rd.getString("autoNo"));
        auto.setOwnerName(rd.getString("ownerName"));
        auto.setSource(rd.getInteger("source"));
        PageUtils.page(rd);
        PageHelper.orderBy(rd.getOrderBy());
        List<Auto> list = autoMapper.selectActive(auto);
        for (Auto at : list) {
            autoProducer.addAuto(at);
        }
        return list;
    }

    @Transactional(rollbackFor = Exception.class)
    public void addAuto(Auto auto) {
        autoMapper.insertSelective(auto);
        autoProducer.addAuto(auto);
        //add from qinze tip:添加车辆保险
        //如果有保险订单号 则表示有车辆保险
        if (!StringUtils.isBlank(auto.getInsurNum())) {
            insuranceService.addCarInsurance(auto);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateAuto(Auto auto) {
        autoMapper.updateByPrimaryKeySelective(auto);

        autoProducer.updateAuto(auto);
    }

    @Transactional(rollbackFor = Exception.class)
    public void removeAuto(Long kid) {
        Auto auto = new Auto();
        auto.setKid(kid);
        auto.setStatus(Cons.COMMON_STATUS_DELETE);
        autoMapper.updateByPrimaryKeySelective(auto);

        autoProducer.deleteAuto(auto);
    }

    public int autoCount() {
        return autoMapper.selectCountActive(new Auto());
    }


    @Transactional(rollbackFor = Exception.class)
    public void bindDriver(Long autoId, Long driverId) {
        driverService.bindDriver(autoId, driverId);
    }

    @Transactional(rollbackFor = Exception.class)
    public void unBindDriver(Long autoId, Long driverId) {
        driverService.unBindDriver(autoId, driverId);
    }

}
